import { _decorator, Component, Node, UIOpacity } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('LightShakeAnim')
export class LightShakeAnim extends Component {
    @property
    shakeCd = 0.5;
    @property
    minOpacity = 0;

    @property
    isResetTime = false;

    curTime = 0;
    uiOpacity: UIOpacity = null;

    protected onLoad(): void {
        this.uiOpacity = this.node.getComponent(UIOpacity);
        if (!this.uiOpacity) {
            this.uiOpacity = this.node.addComponent(UIOpacity);
        }
        this.uiOpacity.opacity = 255;
    }
    protected onEnable(): void {
        if (this.isResetTime) {
            this.curTime = 0;
        }
    }

    update(dt: number) {

        this.curTime += dt;
        if (this.curTime >= this.shakeCd) {
            this.curTime = 0;
        }
        let r = Math.max(Math.sin((this.curTime / this.shakeCd) * Math.PI), 0.3);
        this.uiOpacity.opacity = Math.floor(255 * r) + this.minOpacity;
    }
}

